package com.netease.nie.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.netease.nie.base.controllers.BaseController;
import com.netease.nie.base.exceptions.BusinessException;
import com.netease.nie.base.protocol.SystemResponseCode;
import com.netease.nie.dto.RobGoodsDetailDto;
import com.netease.nie.dto.RobGoodsInfoPeriodSnapshotDto;
import com.netease.nie.dto.RobGoodsPeriodComputeResultSnapshotDto;
import com.netease.nie.dto.RobGoodsPeriodPersonTimeRecordDto;
import com.netease.nie.dto.RobGoodsSnapshotDto;
import com.netease.nie.facade.RobGoodsFacade;

@RestController
@RequestMapping("/api/rob_goods")
public class RobGoodsController extends BaseController {

	@Autowired
	private RobGoodsFacade robGoodsFacade;

	@RequestMapping(value = "/top_list", method = RequestMethod.GET, produces = "application/json")
	public String topList() throws Exception {
		List<RobGoodsSnapshotDto> robGoodsSnapshotDtos = robGoodsFacade
				.queryTopRobGoodsSnapshotDtos();
		return jsonSuccess(robGoodsSnapshotDtos);
	}

	@RequestMapping(value = "/more_list", method = RequestMethod.GET, produces = "application/json")
	public String moreList(Integer page) throws Exception {
		if (page == null)
			page = 1;

		if (page <= 0)
			throw new BusinessException(
					SystemResponseCode.PARAM_VALUE_ILLEGAL.getCode(),
					SystemResponseCode.PARAM_VALUE_ILLEGAL.getMsg());
		List<RobGoodsSnapshotDto> robGoodsSnapshotDtos = robGoodsFacade
				.queryMoreRobGoodsSnapshotDtos(page);
		return jsonSuccess(robGoodsSnapshotDtos);
	}

	@RequestMapping(value = "/period", method = RequestMethod.GET, produces = "application/json")
	public String period(
			@RequestParam(name = "display_period", required = false) Integer displayPeriod,
			@RequestParam(name = "page", defaultValue = "1") Integer page)
			throws Exception {
		if (displayPeriod == null)
			throw new BusinessException(
					SystemResponseCode.PARAM_VALUE_ILLEGAL.getCode(),
					SystemResponseCode.PARAM_VALUE_ILLEGAL.getMsg());
		
		if (page == null)
			page = 1;
		
		if (page <= 0)
			throw new BusinessException(
					SystemResponseCode.PARAM_VALUE_ILLEGAL.getCode(),
					SystemResponseCode.PARAM_VALUE_ILLEGAL.getMsg());
		
		RobGoodsPeriodPersonTimeRecordDto robGoodsPeriodPersonTimeRecordDto = robGoodsFacade
				.queryRobGoodsPeriodPersonTimeRecordDtoByDisplayPeriod(
						displayPeriod, page);
		return jsonSuccess(robGoodsPeriodPersonTimeRecordDto);
	}

	@RequestMapping(value = "/previous_published", method = RequestMethod.GET, produces = "application/json")
	public String previousPublished(
			@RequestParam(name = "rob_goods_id", required = false) Integer robGoodsId)
			throws Exception {
		if (robGoodsId == null)
			throw new BusinessException(
					SystemResponseCode.PARAM_VALUE_ILLEGAL.getCode(),
					SystemResponseCode.PARAM_VALUE_ILLEGAL.getMsg());
		List<RobGoodsInfoPeriodSnapshotDto> robGoodsInfoPeriodDtos = robGoodsFacade
				.queryRobGoodsInfoPeriodSnapshotDtosByRobGoodsId(robGoodsId);
		return jsonSuccess(robGoodsInfoPeriodDtos);
	}

	@RequestMapping(value = "/compute_result", method = RequestMethod.GET, produces = "application/json")
	public String computeResult(
			@RequestParam(name = "display_period", required = false) Integer displayPeriod)
			throws Exception {
		if (displayPeriod == null)
			throw new BusinessException(
					SystemResponseCode.PARAM_VALUE_ILLEGAL.getCode(),
					SystemResponseCode.PARAM_VALUE_ILLEGAL.getMsg());
		RobGoodsPeriodComputeResultSnapshotDto robGoodsPeriodComputeResultSnapshotDto = robGoodsFacade.queryRobGoodsPeriodComputeResultSnapshotDtoByDisplayPeriod(displayPeriod);
		return jsonSuccess(robGoodsPeriodComputeResultSnapshotDto);
	}

	@RequestMapping(value = "/info", method = RequestMethod.GET, produces = "application/json")
	public String info(
			HttpServletRequest request,
			@RequestParam(name = "goods_id", required = false) Integer goodsId,
			@RequestParam(name = "rob_goods_id", required = false) Integer robGoodsId,
			@RequestParam(name = "display_period", required = false) Integer displayPeriod)
			throws Exception {
		if (goodsId == null)
			throw new BusinessException(
					SystemResponseCode.PARAM_VALUE_ILLEGAL.getCode(),
					SystemResponseCode.PARAM_VALUE_ILLEGAL.getMsg());
		
		if (robGoodsId == null)
			throw new BusinessException(
					SystemResponseCode.PARAM_VALUE_ILLEGAL.getCode(),
					SystemResponseCode.PARAM_VALUE_ILLEGAL.getMsg());
		
		if (displayPeriod == null)
			throw new BusinessException(
					SystemResponseCode.PARAM_VALUE_ILLEGAL.getCode(),
					SystemResponseCode.PARAM_VALUE_ILLEGAL.getMsg());
		Long userId = (Long) request.getAttribute("userId");
		
		RobGoodsDetailDto robGoodsDetailDto = robGoodsFacade.queryRobGoodsDetailDtoByCondition(userId, goodsId, robGoodsId, displayPeriod);
		return jsonSuccess(robGoodsDetailDto);
	}
	
}
